package com.zyh.controller;

import com.zyh.entity.*;
import com.zyh.service.*;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class AdminController {
    @Resource
    private StudentService studentService;

    @Resource
    private UserService userService;
    @Resource
    private RolesService rolesService;
    @Resource
    private NACheckService naCheckService;
    @Resource
    private HTableService hTableService;
    @Resource
    private ESchoolService eSchoolService;
    @RequestMapping("/workerManage")
    public String workerManage(Authentication authentication, Model model){
        Roles roles=rolesService.findByAuthority("ROLE_WORKER");
        List<Users> users=userService.findByRoles(roles);
        model.addAttribute("users",users);
        return "adminView/workerManage";

    }
    @RequestMapping("/sHeathyReport")
    public String sHeathyReport(Model model){
        List<HTable> hTables=hTableService.findAll();
        model.addAttribute("hTables",hTables);
        return "adminView/sHeathyReport";
    }
    @RequestMapping("/sESchoolSituation")
    public String sESchoolSituation(Model model){
        List<EnterSchool> enterSchools=eSchoolService.findAllESchool();
        model.addAttribute("enterSchools",enterSchools);
        return "adminView/sESchoolSituation";
    }
    @RequestMapping("/NACheckSituation")
    public String NACheckSituation(Model model){
        List<NACheck> naChecks=naCheckService.findAll();

        model.addAttribute("naChecks",naChecks);
        return "adminView/NACheckSituation";
    }

    @RequestMapping("/wUpdateView")
    public String wUpdateView(@RequestParam("id")String id,Model model){
        Users users=userService.findById(Integer.parseInt(id));
        model.addAttribute("users",users);
        return "adminView/wUpdateView";
    }

    @RequestMapping("/wUpdate")
    public String wUpdate(@RequestParam("id")String id,@RequestParam("wPlace")String wplace){
        Users users=userService.findById(Integer.parseInt(id));
        users.setwPlace(wplace);
        userService.save(users);
        return "adminView/WUSuccess";
    }

    @RequestMapping("/findWorker")
    public String findWorker(@RequestParam("info") String info, Model model){
        Roles roles=rolesService.findByAuthority("ROLE_WORKER");
        Users users = userService.findByUsernameAndRoles(info, roles);
        if (users ==null){
            users =userService.findByNameAndRoles(info,roles);
            if (users ==null){
                return "adminView/error";
            }
        }
        model.addAttribute("users", users);
        return "adminView/findWorker";
    }

    @RequestMapping("/findSH")
    public String findSH(@RequestParam("info") String info, Model model){
        List<HTable> hTables;
        if (isNumeric(info)) {
            hTables = hTableService.findBySno(Integer.parseInt(info));
            model.addAttribute("hTables", hTables);
            return "adminView/sHeathyReport";
        }else{
            return "adminView/error";
        }

    }
    @RequestMapping("/findSE")
    public String findSE(@RequestParam("info") String info, Model model){
        List<EnterSchool> enterSchools;
        if (isNumeric(info)){
            enterSchools =eSchoolService.findAllBySno(Integer.parseInt(info));
        }else{
            enterSchools = eSchoolService.findAllBySname(info);
        }
        model.addAttribute("enterSchools",enterSchools);
        return "adminView/sESchoolSituation";
    }
    @RequestMapping("/findNA")
    public String findNA(@RequestParam("info") String info, Model model){
        List<NACheck> naChecks;
        if (isNumeric(info)){
            naChecks = naCheckService.findAllBySno(Integer.parseInt(info));
        }else{
            naChecks =naCheckService.findAllBySname(info);
        }
        model.addAttribute("naChecks", naChecks);
        return "adminView/NACheckSituation";
    }

    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
}
